package com.rdc.example.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.rdc.example.util.DBUtil.closeResources;
import static com.rdc.example.util.DBUtil.getConnection;

public class FollowDaoImpl implements com.rdc.example.dao.FollowDao {

    @Override
    public boolean insertFollow(Integer followerId, Integer followedId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //获取连接
            conn = getConnection();
            String sql = "insert into `follow` (follower_id, followed_id) values (?, ?)";
            pstmt = conn.prepareStatement(sql);
            //设值
            pstmt.setInt(1,followerId);
            pstmt.setInt(2, followedId);
            //按照影响行数row返回结果
            int row = pstmt.executeUpdate();
            return row > 0;
        } catch (SQLException e) {
            if (e.getErrorCode() == 1062) {
                System.out.println("重复关注: followerId=" + followerId + ", followedId=" + followedId);
            } else {
                throw e;
            }
        } finally {
            //释放资源
            closeResources(null, pstmt, conn);
        }
        return false;
    }
    @Override
    public boolean deleteFollow(Integer followerId, Integer followedId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //获取连接
            conn = getConnection();
            String sql = "delete from `follow` where follower_id=? and followed_id=? ";
            pstmt = conn.prepareStatement(sql);
            //设值
            pstmt.setInt(1, followerId);
            pstmt.setInt(2, followedId);
            pstmt.executeUpdate();
            // 只要SQL执行无异常，均返回true
            return true;
        } finally {
            //释放资源
            closeResources(null, pstmt, conn);
        }
    }

    @Override
    public boolean check(Integer followerId, Integer followedId) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            // 获取连接
            conn = getConnection();
            // 定义查询语句 查询有没有时select 1 比count 更快
            String sql = "select 1 from `follow` where follower_id =? and followed_id =?";
            pstmt = conn.prepareStatement(sql);
            // 设置参数
            pstmt.setInt(1, followerId);
            pstmt.setInt(2, followedId);
            // 执行查询操作
            rs = pstmt.executeQuery();
            // 如果结果集有下一行，说明存在记录，即播放列表中存在该歌曲
            return rs.next();
        } finally {
            // 释放资源
            closeResources(rs, pstmt, conn);
        }
    }

    @Override
    public List<Integer> listUserIdByFollowerId(Integer followerId) throws Exception {
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Integer> songIdList = new ArrayList<>();
        try {
            String sql = "select followed_id from `follow` where follower_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, followerId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                //循环获取结果
                Integer songId = rs.getInt("followed_id");
                songIdList.add(songId);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 释放资源
            closeResources(rs, pstmt, conn);
        }
        return songIdList;
    }

    @Override
    public List<Integer> listUserIdByFollowedId(Integer followedId) throws Exception {
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Integer> songIdList = new ArrayList<>();
        try {
            String sql = "select follower_id from `follow` where followed_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, followedId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                //循环获取结果
                Integer songId = rs.getInt("follower_id");
                songIdList.add(songId);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 释放资源
            closeResources(rs, pstmt, conn);
        }
        return songIdList;
    }




}
